#!/bin/sh

revision="setregeu v0.1.6 (2023-07-02 by MIB-Wiki)"
# use --help for more info

export PATH=:/proc/boot:/sbin:/bin:/usr/bin:/usr/sbin:/net/mmx/bin:/net/mmx/usr/bin:/net/mmx/usr/sbin:/net/mmx/sbin:/net/mmx/mnt/app/armle/bin:/net/mmx/mnt/app/armle/sbin:/net/mmx/mnt/app/armle/usr/bin:/net/mmx/mnt/app/armle/usr/sbin
export LD_LIBRARY_PATH=/net/mmx/mnt/app/root/lib-target:/net/mmx/mnt/eso/lib:/net/mmx/eso/lib:/net/mmx/mnt/app/usr/lib:/net/mmx/mnt/app/armle/lib:/net/mmx/mnt/app/armle/lib/dll:/net/mmx/mnt/app/armle/usr/lib
export IPL_CONFIG_DIR=/etc/eso/production

thisname="$(basename $0)"
thisdir="$(dirname $0)"

if [ -z $LOG ]; then
	. $thisdir/../config/GLOBALS
fi
echo -ne "$ME-$thisname---->\n" | $TEE -i -a $LOG

if [ -f $TMP/reboot.mib ]; then
	echo -ne "Aborted because the reboot is running...\n" | $TEE -a $LOG
	return 2> /dev/null
fi

MIBCAP=3
. $thisdir/../config/MIBCHECK

if [ "$2" = "-noboot" ]; then
	noboot=$2
else
	noboot=""
fi

#Get the real train not from GLOBALS but from EEPROM as it can be changed already
TRAINVERSION="$($E2P r 3A0 19 | $SED -rn 's/^0x\S+\W+(.*?)$/\1/p' | $SED -rn 's:\W*(\S\S)\W*:0x\1\n:pg' | $SED -rn '/^0x/p' | $XXD -r -p | $SED 's/[^a-zA-Z0-9_-]//g' )"

case $1 in
	-eu) {
		trap '' 2
		mount -uw /net/rcc/mnt/efs-persist
		if [[ "$TRAINVERSION" = *ER* || "$TRAINVERSION" = *EU* ]]; then
			echo -ne "Writing EU language datasets to 5F...\n" | $TEE -a $LOG
			/net/mmx/fs/sda0/apps/setlang -eu -noboot
			echo -ne "\nChanging coding and adaptations to EU:" | $TEE -a $LOG
			echo -ne "\n--- Summer time algo\n" | $TEE -a $LOG
			# 0=none, 1=manual, 2=MESZ, 3=USA
			$PERSW -f -t int 28442848 503 2
			
			echo -ne "--- FM Band setting\n" | $TEE -a $LOG
			# 0=BAND_FM_NO_SETTING, 1=BAND_FM_EU_RDW, 2=BAND_FM_NAR, 3=BAND_FM_JP, 4=BAND_FM_KOR
			$PERSW -f -t int 0 1343759375 1
			
			echo -ne "--- AM Band setting\n" | $TEE -a $LOG
			# 0=BAND_AM_NO_SETTING, 1=BAND_AM_EU_RDW, 2=BAND_AM_NAR, 3=BAND_AM_JP, 4=BAND_AM_EU, 5=BAND_AM_AUS
			$PERSW -f -t int 0 1343759600 1
			
			#echo -ne "--- DAB Bandsetting 1\n"
			# 0=BAND_DAB1_OFF, 1=BAND_DAB1_EU_BAND_III-N, 2=BAND_DAB1_EU_BAND_III (EU default), 3=BAND_DAB1_CANADA_L_BAND
			# 4=BAND_DAB1_KOREA_BAND_III, 5=BAND_DAB1_CHINA_BAND_III, 6=BAND_DAB1_DOWNLOAD_TABLE_1, 7=BAND_DAB1_NEW_ZEALAND_BAND_III
			#$PC i:0:0x5018280F 0

			#echo -ne "--- DAB Bandsetting 2\n"
			# 0=BAND_DAB2_OFF, 1=BAND_DAB2_L_BAND (EU default), 2=BAND_DAB2_DOWNLOAD_TABLE_2
			#$PC i:0:0x501828F0 0

			echo -ne "--- RDS switch off\n" | $TEE -a $LOG
			$PERSW -f -t int 0 1343763457 0 2>> $LOG

			echo -ne "--- AF functionality on\n" | $TEE -a $LOG
			$PERSW -f -t int 0 1343763458 1 2>> $LOG
			
			echo -ne "--- Radiotext+ on\n" | $TEE -a $LOG
			$PERSW -f -t int 0 1343763464 1 2>> $LOG

			echo -ne "--- PI Ignore off\n" | $TEE -a $LOG
			$PERSW -f -t int 0 1343763472 0 2>> $LOG

			echo -ne "--- AM disable on\n" | $TEE -a $LOG
			$PERSW -f -t int 0 1343764482 1 2>> $LOG

			echo -ne "--- RDS deactivation off\n" | $TEE -a $LOG
			$PERSW -f -t int 0 1343764496 0 2>> $LOG

			echo -ne "--- AF deactivation off\n" | $TEE -a $LOG
			$PERSW -f -t int 0 1343764512 0 2>> $LOG

			echo -ne "--- Navigation Country\n" | $TEE -a $LOG
			# 1=EU, 2=NAR, 3=MSA, 4=Korea, 5=China, 6=Japan, 7=AsiaPacific, 8=Australia, 9=South Africa, 10=NEAST,
			# 11=NMAfrica, 12=MEAST, 13=CentralAsia, 14=India, 15=Israel, 16=Taiwan, 17=MSA 2 (Chile)
			$PERSW -f -t int 0 1343753471 1 2>> $LOG

			echo -ne "--- Navigation on\n" | $TEE -a $LOG
			$PERSW -f -t int 0 1343774724 1 2>> $LOG

			echo -ne "--- Import media\n" | $TEE -a $LOG
			$PERSW -f -t int 0 1343774736 1 2>> $LOG

			echo -ne "--- Media Ripping\n" | $TEE -a $LOG
			$PERSW -f -t int 0 1343774752 1 2>> $LOG

			echo -ne "--- Traffic signs on the map\n" | $TEE -a $LOG
			$PERSW -f -t int 0 1343774784 1 2>> $LOG

			echo -ne "--- Regional Codes DVD\n" | $TEE -a $LOG
			$PERSW -f -t int 28442848 515 2 2>> $LOG

			echo -ne "--- Regional Codes Blueray\n" | $TEE -a $LOG
			$PERSW -f -t int 28442848 516 2 2>> $LOG

			echo -ne "--- Regional Codes DVD Counter\n" | $TEE -a $LOG
			$PERSW -f -t int 28442848 517 5 2>> $LOG

			echo -ne "--- Regional Codes Blueray Counter\n" | $TEE -a $LOG
			$PERSW -f -t int 28442848 518 5 2>> $LOG
			
			echo -ne "--- TMCPro EU\n" | $TEE -a $LOG
			$PERSW -f -t int 28442848 520 32783 2>> $LOG

			echo -ne "--- Gracenote local ON\n" | $TEE -a $LOG
			$PERSW -f -P -L 1 -O 9 0 3221356628 F 2>> $LOG
			$PERSW -f -P -L 1 -O 8 0 3221356628 F 2>> $LOG

			echo -ne "--- Radio stationlogo DB\n" | $TEE -a $LOG
			#	0=none, 1=EU, 28=China, 66=Hongkong, Macao=67, CD=Korea, EA=Taiwan
			$PERSW -f -P -L 8 -O 48 0 3221356628 01 2>> $LOG
			
			echo -ne "--- SDS region Flag\n" | $TEE -a $LOG
			# 0=no region, 1=EU, 2=NAR, 3=MSA, 4=Korea, 5=China, 6=Japan, 7=AsiaPacific, 8=Australia, 9=South Africa, 10=NEAST,
			# 11=NMAfrica, 12=MEAST, 13=CentralAsia, 14=India, 15=Israel, 16=Taiwan, 17=MSA 2 (Chile), 18=China 2, 19=China 3
			$PERSW -f -P -L 8 -O 72 0 3221356628 01 2>> $LOG

			echo -ne "--- Variant region\n" | $TEE -a $LOG
			# 1=Europe/ROW, 2=Europe, 3=NAR, 4=ROW, 5=China, 6=Japan, 7=Korea, 8=Asia, 9=Taiwan
			$PERSW -f -t int 678364556 9 2 2>> $LOG
			$E2P w E0 02 >> $LOG

			#echo -ne "switch variant feature byte type to High\n"
			#$PC i:678364556:4 1

			echo -ne "--- Disable variant feature byte sirius\n"
			$PERSW -f -t int 678364556 8 0 2>> $LOG

			CODING="$($E2P r F1 19 | $SED -rn 's/^0x\S+\W+(.*?)$/\1/p' | $SED -rn 's:\W*(\S\S)\W*:0x\1\n:pg' | $SED -rn '/^0x/p'  | $SED -e 's/0x//g' | $SED -e ':a;N;$!ba;s/\n//g')"
			echo "5F Long Coding: $CODING" >> $LOG

			echo -ne "\n$thisname done!\n" | $TEE -i -a $LOG
		else
			echo -ne "Skipped as unit has non EU train.\n" | $TEE -a $LOG
			exit 1
		fi

trap 2
[ -z "$noboot" ] && . $thisdir/reboot -t 10
[ -z "$noboot" ] && return 2> /dev/null

	};;

	# help or unknown parameter ------------------------------
	*) {
		echo ""
		echo $revision
		echo ""
		echo "Usage: "$thisname" [OPTION] [-noboot]"
		echo ""
		echo "Options:"
		echo "        -eu           set Region to EU"
		echo "        --help        show this help"
		echo ""
		echo "Note: Backup will only work in RCC bash!"
		echo ""
		echo "This program is free software; you can redistribute it and/or"
		echo "modify it under the terms of the GNU General Public License"
		echo "as published by the Free Software Foundation; either version 2"
		echo "of the License, or (at your option) any later version."
		echo ""
		echo "This program is distributed in the hope that it will be useful,"
		echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
		echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
		echo "See the GNU General Public License for more details."
		echo ""
		echo "You should have received a copy of the GNU General Public License"
		echo "along with this program; if not, write to the Free Software Foundation,"
		echo "Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA."
		echo ""
	};;
esac

exit 0
